#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
#include <netinet/in.h>
#include <string.h>

/** 
 * Client sends only one message to server,
 * gets an answer and closes itself.
 */

int main() {
  int sock;
  struct sockaddr_in addr;
  char buf[1024];
  char message[1024];
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socket");
    return errno;
  }
  bzero(&addr, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(8888);
  #define SERVER_ADDR "127.0.0.1"
  inet_aton(SERVER_ADDR, &addr.sin_addr.s_addr);
  if (connect(sock, (struct sockaddr *)&addr, 
              sizeof(addr)) < 0) {
    perror("connect");
    return errno;
  }
  scanf("%[^\n]s", message);
  send(sock, message, sizeof(message), 0);
  recv(sock, buf, sizeof(message), 0);
  printf("%s\n", buf);
  close(sock);
  return 0;
}
